<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .dahezi {
        position: relative;
        width: 400px;
        height: 400px;
        top: 100px;
        margin: auto;
        overflow: hidden;
    }

    .dahezi .ul1 {
        position: absolute;
        width: 1600%;
        height: 100%;
        background-color: aqua;
        opacity: 0.1;

    }

    .ul1 li {
        float: left;
        list-style: none;
        width: 400px;
        height: 100%;
        font-weight: 1000;
        font-size: 100px;
        color: red;
    }

    .left {
        position: absolute;
        z-index: 9;
        width: 40px;
        height: 40px;
        opacity: 0.5;
        top: 50%;
        transform: translate(0, -50%);
        display: none;
    }

    .right {
        position: absolute;
        z-index: 9;
        right: 0;
        width: 40px;
        height: 40px;
        opacity: 0.5;
        top: 50%;
        transform: translate(0, -50%);
        display: none;
    }

    .ol1 {
        position: absolute;
        bottom: 0;
        left: 15%;
    }

    .ol1 li {
        float: left;
        list-style: none;
        margin-right: 10px;
        border-radius: 45%;
        cursor: default;
    }

    .current {
        background-color: grey;
    }
</style>

<body>
    <div class="dahezi">
        <button class="left">&#60</button>
        <ul class="ul1">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
        </ul>
        <ol class="ol1">
        </ol>
        <button class="right"> > </button>
    </div>
    <script>
        var dahezi = document.querySelector('.dahezi');
        var btn1 = document.querySelector('.left');
        var btn2 = document.querySelector('.right');
        var ul1 = document.querySelector('.ul1')
        var ol1 = document.querySelector('.ol1')
        var flag = true;
        //动态创建小圆点
        for (var i = 0; i < ul1.children.length; i++) {
            //创建li圆点
            var li = document.createElement('li');
            li.innerHTML = 'O';
            //给li创建索引号
            li.setAttribute('index', i);
            //插入ol
            ol1.appendChild(li);
            //小圆点变色事件和移动事件绑定，在每个小li创建的时候就绑定了
            li.addEventListener('click', function () {
                //排他思想
                for (var i = 0; i < ol1.children.length; i++) {
                    ol1.children[i].className = '';
                }
                //留下我自己,此处一定要用this，用li是错误的
                this.className = 'current';
                //获取索引号从而移动距离
                if (count == ol1.children.length && this.getAttribute('index') == 0) {
                    ;
                } else {
                    move(ul1, -this.getAttribute('index') * dahezi.offsetWidth);
                    count = this.getAttribute('index');
                }
            })
        }
        //把第一个小圆点变色
        ol1.children[0].className = 'current';
        //在末尾创造和第一个图一样的图
        var li = document.createElement('li');
        ul1.appendChild(li);
        li.innerHTML = '1';
        //点击按钮移动
        var count = 0;
        btn1.addEventListener('click', function () {
            if (flag == true) {
                flag = false;
                if (count == 0) {
                    count = ul1.children.length - 1;
                    ul1.style.left = -count * dahezi.offsetWidth + 'px';
                }
                count--;
                move(ul1, -count * dahezi.offsetWidth, function () {
                    flag = true;
                });
                for (var i = 0; i < ol1.children.length; i++) {
                    ol1.children[i].className = '';
                }
                ol1.children[count % ol1.children.length].className = 'current';
            }
        })
        btn2.addEventListener('click', function () {
            if (flag == true) {
                flag = false;
                if (count == ol1.children.length) {
                    count = 0;
                    ul1.style.left = 0;
                }
                count++;
                move(ul1, -count * dahezi.offsetWidth, function () {
                    flag = true;
                });
                for (var i = 0; i < ol1.children.length; i++) {
                    ol1.children[i].className = '';
                }
                ol1.children[count % ol1.children.length].className = 'current';
            }
        })
        //自动播放功能
        var timer = setInterval(function () {
            btn2.click();
        }, 2000)
        //鼠标经过停止播放
        dahezi.addEventListener('mouseover', function () {
            btn1.style.display = 'block';
            btn2.style.display = 'block';
            clearInterval(timer);
        })
        //鼠标离开开始播放
        dahezi.addEventListener('mouseout', function () {
            btn1.style.display = 'none';
            btn2.style.display = 'none';
            timer = setInterval(function () {
                btn2.click();
            }, 2000)
        })

        //图片移动函数
        function move(obj, target, back) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                var speed = ((target - obj.offsetLeft) / 10);
                if (speed > 0) {
                    speed = Math.ceil((target - obj.offsetLeft) / 10);
                } else {
                    speed = Math.floor((target - obj.offsetLeft) / 10);
                }
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                    back && back();
                }
                obj.style.left = obj.offsetLeft + speed + 'px';
            }, 15)
        }
    </script>
</body>

</html>